import router from '@/router'
import { getToken } from '@/utils/auth'

const whiteList = ['/login']  // no redirect whitelist

router.beforeEach(async (to, from, next) => {
    // to and from are both route objects. must call `next`.
    const hasToken = getToken()
    if (hasToken) {
        if(to.path === '/login') {
            next({ path: '/' })
        } else {
            // const hasUserInfo = user.name
            // if (hasUserInfo) {
            //     next()
            // } else {
            //     try {
            //         get user info
            //          await store.dispath('getuserInfo')
            //         next()
            //     } catch (error) {
            //         await sotre.dispath('getToken')
            //          next('/')
            //     }
            // }
            next()
        }

    } else {
        /*  no Token  */
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
        }
    }
})

router.afterEach((to, from) => {
    // to and from are both route objects.
})